package com.scut.chenlujie.library.clink.core;

import java.io.Closeable;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SocketChannel;

public interface IoProvider extends Closeable {
    //如果可以input或者output , 就回调
    boolean registerInput(SocketChannel channel ,HandleProviderCallback callback) throws ClosedChannelException;
    //已有数据，但是因为网卡等原因channel并没有准备 , 所以可以先把准备好的数据放上去，如果channel准备好了就可以直接发
    boolean registerOutput(SocketChannel channel , HandleProviderCallback callback);
    void unregisterInput(SocketChannel channel );
    void unregisterOutput(SocketChannel channel );

    //抽象类：处理注册inputChannel 和 outputChannel的回调 继承自Runnable -> 可以放到线程池调度


    //任务：可以放到线程池调度
    abstract class HandleProviderCallback implements Runnable {
        protected volatile IoArgs attach;

        public final void run(){
            onProviderIo(attach);
        }

        protected abstract void onProviderIo(IoArgs args);

        public  void checkAttachNull(){
            if(attach != null){
                throw new IllegalStateException("非法状态异常");
            }
        }
    }
}
